package org.gdkm.springbootxiangmushizhan.mapper;

import org.apache.ibatis.annotations.*;
import org.gdkm.springbootxiangmushizhan.domain.po.Comment;
import org.gdkm.springbootxiangmushizhan.domain.vo.CommentAdminVO;

import java.util.List;
@Mapper
public interface CommentMapper {
//    分页展示某个文章的评论
    @Select("select * from t_comment where article_id = #{aid} AND t_comment.status = 'approved' order by id desc")
    public List<Comment> selectCommentWithPage(Integer aid);
//    后台查询最新几条评论
    @Select("select * from t_comment order by id desc")
    public List<Comment> selectNewComment();
//    发表评论
    @Insert("insert into t_comment (article_id,created,author,ip,content)"+
            "values (#{articleId},#{created},#{author},#{ip},#{content})")
    public void pushComment(Comment comment);
//    站点服务统计，统计评论数量
    @Select("select count(1) from t_comment")
    public Integer countComment();
//    通过文章id删除评论
    @Delete("delete from t_comment where article_id = #{aid}")
    public void deleteCommentWithId(Integer aid);

    // 分页查询所有评论及文章标题
    @Select("SELECT c.id, c.author, c.created, c.content, c.status, a.title AS articleTitle, c.article_id AS articleId FROM t_comment c LEFT JOIN t_article a ON c.article_id = a.id ORDER BY c.id DESC")
    List<CommentAdminVO> selectAllCommentsWithArticleTitle();

    // 根据评论id删除评论
    @Delete("DELETE FROM t_comment WHERE id = #{id}")
    void deleteCommentById(Integer id);

    // 根据评论id修改status
    @Update("UPDATE t_comment SET status = #{status} WHERE id = #{id}")
    void updateCommentStatusById(@Param("id") Integer id, @Param("status") String status);
}
